package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import com.rla.domain.BaseHealthTargetRule;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseDrugSpeciBo;
import com.rla.domain.vo.BaseDrugSpeciVo;
import com.rla.domain.BaseDrugSpeci;
import com.rla.mapper.BaseDrugSpeciMapper;
import com.rla.service.IBaseDrugSpeciService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 药品-规格对照Service业务层处理
 *
 * @author rla
 * @date 2025-08-04
 */
@RequiredArgsConstructor
@Service
public class BaseDrugSpeciServiceImpl implements IBaseDrugSpeciService {

    private final BaseDrugSpeciMapper baseMapper;

    /**
     * 查询药品-规格对照
     */
    @Override
    public BaseDrugSpeciVo queryById(String id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询药品-规格对照
     */
    @Override
    public BaseDrugSpeciVo queryBySkuCode(String skuCode){
        LambdaQueryWrapper<BaseDrugSpeci> lqw = Wrappers.lambdaQuery();
        lqw.eq(BaseDrugSpeci::getSkuCode, skuCode);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询药品-规格对照列表
     */
    @Override
    public TableDataInfo<BaseDrugSpeciVo> queryPageList(BaseDrugSpeciBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDrugSpeci> lqw = buildQueryWrapper(bo);
        Page<BaseDrugSpeciVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品-规格对照列表
     */
    @Override
    public List<BaseDrugSpeciVo> queryList(BaseDrugSpeciBo bo) {
        LambdaQueryWrapper<BaseDrugSpeci> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDrugSpeci> buildQueryWrapper(BaseDrugSpeciBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDrugSpeci> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), BaseDrugSpeci::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), BaseDrugSpeci::getSkuCode, bo.getSkuCode());
        lqw.like(StringUtils.isNotBlank(bo.getSpeciName()), BaseDrugSpeci::getSpeciName, bo.getSpeciName());
        lqw.like(StringUtils.isNotBlank(bo.getErpProductName()), BaseDrugSpeci::getErpProductName, bo.getErpProductName());
        lqw.like(StringUtils.isNotBlank(bo.getErpSpeciName()), BaseDrugSpeci::getErpSpeciName, bo.getErpSpeciName());
        lqw.eq(StringUtils.isNotBlank(bo.getErpSpeciCode()), BaseDrugSpeci::getErpSpeciCode, bo.getErpSpeciCode());
        lqw.eq(bo.getErpStockNum() != null, BaseDrugSpeci::getErpStockNum, bo.getErpStockNum());
        lqw.eq(bo.getSalesPrice() != null, BaseDrugSpeci::getSalesPrice, bo.getSalesPrice());
        lqw.eq(bo.getDashingPrice() != null, BaseDrugSpeci::getDashingPrice, bo.getDashingPrice());
        lqw.eq(bo.getCostPrice() != null, BaseDrugSpeci::getCostPrice, bo.getCostPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getPicUrl()), BaseDrugSpeci::getPicUrl, bo.getPicUrl());
        lqw.eq(bo.getExchangePoints() != null, BaseDrugSpeci::getExchangePoints, bo.getExchangePoints());
        lqw.eq(StringUtils.isNotBlank(bo.getIsExchange()), BaseDrugSpeci::getIsExchange, bo.getIsExchange());
        lqw.eq(bo.getParticipationNum() != null, BaseDrugSpeci::getParticipationNum, bo.getParticipationNum());
        lqw.eq(bo.getSort() != null, BaseDrugSpeci::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增药品-规格对照
     */
    @Override
    public Boolean insertByBo(BaseDrugSpeciBo bo) {
        BaseDrugSpeci add = BeanUtil.toBean(bo, BaseDrugSpeci.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改药品-规格对照
     */
    @Override
    public Boolean updateByBo(BaseDrugSpeciBo bo) {
        BaseDrugSpeci update = BeanUtil.toBean(bo, BaseDrugSpeci.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDrugSpeci entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品-规格对照
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
